<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:dswp 積分管理
 *@param datetime 2014-01-12
 *@param author abin
 ********************/
class Integral extends Admins_Controller {
  
  private $data=array();
  private $unique_id;

  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='orders';
    $this->load->model('integral_mdl');
    $this->data['format_time']=$this->settings['default_time'];
	$this->data['lang'] =$this->lang->language;
  }

  public function index(){
    $this->_get_list();
    // $this->output->enable_profiler(true);
    $this->load->view('admins/integral_list_view',$this->data);
  } 

  private function _get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    // $limit =3;
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/integral');
    }
    $url_query=$_SERVER['QUERY_STRING'];

    // 保留查詢參數;
    $cookie = array(
        'name'   => 'url_query',
        'value'  => base64_encode($url_query),
        'expire' => '0',
        'path'   => '/',
    );
    $this->input->set_cookie($cookie);

    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }

    $this->data['phone']=$this->input->get('phone');
    
    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'id';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'desc';
    
    $data=array(
      'limits'=>$limit,
      'offset'=>$offset,
      'field'=>$this->data['field'],
      'sort'=>$this->data['sort'],
      'phone'=>$this->data['phone'],
    );

    $this->data['lists_count']=$this->integral_mdl->totle_integral($data);
    
    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->changeClass('pagination');
      $this->dpagination->showCounter(TRUE);
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(2);
      $this->dpagination->target(site_url('admins/integral').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
	  $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->integral_mdl->get_integral($data);
  }

  //對積分進行操作
  public function add($integral_id=''){
    if(!empty($integral_id) && is_numeric($integral_id)){
      $this->data['list_one']=$this->integral_mdl->get_integral_by_id($integral_id);
      $this->data['integral_id']=$integral_id;
      $this->load->view('admins/integral_form_view', $this->data);
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }

  //積分在線充值和扣除
  //
  public function modify(){
    $edit_id=$this->input->post('edit_id');
    $this->_load_validation_rules();
    if ($this->form_validation->run() == FALSE){
      $this->data['error']=true;
      $this->add($edit_id);
    }else{
      $notify=$this->input->post('notify');//計算分數.
      $score=$this->input->post('score');
      $integral=$this->input->post('integral');//原本積分
      $integral_history=array(
        'admin_name'=>$this->nsession->userdata('nick_name'),
        'notify'=>$notify,
        'score'=>$score,
        'comment'=>$this->input->post('comment'),
        'add_time'=>time(),
      );
      $integral_data=array(
        'phone'=>$this->input->post('phone'),
        'update_time'=>time(),
      );
      $result=0;
      if($notify==1){ //新增積分
        $integral_data['integral']=$integral+$score;
        $result=$this->integral_mdl->edit_integral($integral_data,$edit_id);
      }else{ //扣除積分
        if(($integral-$score)>=0){
          $integral_data['integral']=$integral-$score;
        }else{
          $integral_data['integral']=0;
          $integral_history['score']=$integral;
        }
        $result=$this->integral_mdl->edit_integral($integral_data,$edit_id);
      }
      if($result){//積分變化成功,新增 積分變化記錄
        $integral_history['integral_id']=$edit_id;
        $this->load->model('integralhistory_mdl');
        $result+=$this->integralhistory_mdl->add_integralhistory($integral_history);
      }

      $this->operator_log('手動變更積分,變更積分->ID:'.$edit_id,'變更積分',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      $redirect=site_url('admins/integral/view/'.$edit_id);
      redirect($redirect);
    }
  }

  //數據格式驗證
  public function _load_validation_rules(){
    $this->form_validation->set_rules('phone','電話號碼','trim|required|numeric|max_length[20]');
    $this->form_validation->set_rules('integral','目前積分','trim|required|numeric|max_length[10]');
    $this->form_validation->set_rules('notify','增加/扣除','trim|required|numeric|max_length[1]');
    $this->form_validation->set_rules('score','積分','trim|required|numeric|max_length[10]');
    $this->form_validation->set_rules('edit_id','所屬ID','trim|required|numeric|max_length[5]');
    $this->form_validation->set_rules('comment','備註','trim');
  }  

  //執行刪除功能
  public function delete($delete_id=0){
    if(!empty($delete_id)){
      $result=$this->integral_mdl->delete_integral($delete_id);
      $this->operator_log('刪除積分->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect('admins/integral');
    }else{
      show_error('對不起,delete_id參數出錯!');
      exit;
    }
  }

   //批量刪除
  public function delete_batch(){
    $delete_string=$this->input->get('delete_string');
    if(!empty($delete_string)){
      $del_arr=explode(',',$delete_string);
      $result=$this->integral_mdl->delete_batch($del_arr);

      $this->operator_log('批量刪除積分列表->ID:'.$delete_string,'批量刪除',$result);

     $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      $redirect=site_url('admins/integral').$this->settings['url_query'];//獲取列表篩選參數
      redirect($redirect);
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }

  //view
  //查看記錄
  public function view($view_id=''){
    if(!empty($view_id) && is_numeric($view_id)){
      $this->data['list_one']=$this->integral_mdl->get_integral_by_id($view_id);
      $this->_get_integral_history($view_id);//獲取訂單記錄
      $this->load->view('admins/integral_record_view',$this->data);
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }

  //訂單詳情 記錄
  public function _get_integral_history($integral_id){
    if (!empty($integral_id) && is_numeric($integral_id)) {
      $this->load->model('integralhistory_mdl');
      $page = $this->input->get('p',TRUE);
      $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
      $limit = $this->settings['default_admin_limit'];
      // $limit = 3;
      $offset = ($page - 1) * $limit;
      if($offset < 0){
        redirect('admins/integral');
      }
      $url_query=$_SERVER['QUERY_STRING'];

      if(!empty($url_query)){
        $url_query=preg_replace('/&p=(\d+)/','',$url_query);
      }

      $this->data['notify']=$this->input->get('notify');
      $this->data['admin_name']=$this->input->get('admin_name');
      $this->data['begin_month']=$this->input->get('begin_month');
      $this->data['end_month']=$this->input->get('end_month');

      
      $this->data['field']=$this->input->get('field');
      $this->data['sort']=$this->input->get('sort');
      $this->data['field']=!empty($this->data['field'])?$this->data['field']:'add_time';
      $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'desc';
      
      $data=array(
        'limits'=>$limit,
        'offset'=>$offset,
        'field'=>$this->data['field'],
        'sort'=>$this->data['sort'],
        'integral_id'=>$integral_id,
        'notify'=>$this->data['notify'],
        'admin_name'=>$this->data['admin_name'],
        'begin_month'=>!empty($this->data['begin_month'])?strtotime($this->data['begin_month']):'',
        'end_month'=>!empty($this->data['end_month'])?strtotime($this->data['end_month']):'',
      );

      $this->data['lists_count']=$this->integralhistory_mdl->totle_integralhistory($data);
      
      $pagination = '';
      if($this->data['lists_count'] > $limit)
      {
        $this->dpagination->currentPage($page);
        $this->dpagination->items($this->data['lists_count']);
        $this->dpagination->limit($limit);
        $this->dpagination->adjacents(2);
        $this->dpagination->target(site_url('admins/integral/view/'.$integral_id).'?'.$url_query);
        $this->dpagination->parameterName('p');
        $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
        $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
        $pagination = $this->dpagination->getOutput();
      }
      $this->data['pagination'] = $pagination;
      $this->data['lists']=$this->integralhistory_mdl->get_integralhistory($data);
    }
  }

  //刪除積分記錄
  public function delete_history($delete_id=''){
    $integral_id=$this->input->get('integral_id');
    if(!empty($delete_id) && is_numeric($delete_id) && !empty($integral_id)){
      $this->load->model('integralhistory_mdl');
      $result=$this->integralhistory_mdl->delete_integralhistory($delete_id);
      $this->operator_log('刪除積分記錄->ID:'.$delete_id,'刪除',$result);
     $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect('admins/integral/view/'.$integral_id);
    }else{
      show_error('對不起,delete_id參數出錯!');
      exit;
    }
  }
}